﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComLib.ConfigService;
using ComLib.ComType;
using ProcessConfig.ConfigType;
using ProcessConfig;
using LogLib;

namespace CardPhotoQCServer.QC
{
    public class QCProductFactory : IDisposable
    {
        #region static fields
        private static object _lock = new object();

        private static QCProductFactory _instance;
        public static QCProductFactory Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new QCProductFactory();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion

        private bool _bInited = false;
        public bool Inited
        {
            get { return _bInited; }
        }

        private static string _error = "";
        public string Error
        {
            get { return _error; }
        }

        private QCProcess _process = null;
        public QCProcess Process
        {
            get { return _process; }
            set { _process = value; }
        }
     
        protected QCOptions _options = null;    

        private Dictionary<int, QCHandler> Handles = new Dictionary<int, QCHandler>();

        private QCProductFactory()
        {
            _options = Configs.QC;
            Handles.Add(0, new PhotoQCHard_Mdty());
            Handles.Add(1, new PhotoQCHard_Otal());
            Handles.Add(2, new PhotoQCSoft_Mdty());
            Handles.Add(3, new PhotoQCSoft_Otal());
        }

        public bool NeedQCCheck(ApplyTo app)
        {
            if (app == ApplyTo.ExitAndEntry && Configs.QC.CRJ.Enabled)
            {
                return true;
            }
            if (app == ApplyTo.PortVisa && Configs.QC.Visa.Enabled)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 选择合适的PXL句柄和一所操作对象
        /// </summary>
        public bool SelectQCHandler(ApplyTo app)
        {
            if(_process == null)
            {
                return false;
            }
            // 选定合适的一所检测对象
            if (app == ApplyTo.ExitAndEntry && Configs.QC.CRJ.Enabled)
            {
                Log.WriteAsync("用户选择了出入境证件照片类型一所质检");
                _process.Handler = Handles[Configs.QC.CRJ.HandleType()];
                return true;
            }
            else if (app == ApplyTo.PortVisa && Configs.QC.Visa.Enabled)
            {
                Log.WriteAsync("用户选择了签证证件照片类型一所质检");
                _process.Handler = Handles[Configs.QC.Visa.HandleType()];
                return true;
            }
            else
            {
                _process.Handler = null;
                return false;
            }
        }

        public bool InitQCLib(QCOptions opts)
        {
            if (_bInited)
            {
                return true;
            }
            QCCheckOption paraCRJ = opts.CRJ;
            QCCheckOption paraVisa = opts.Visa;
            bool bRet0 = true;
            bool bRet1 = true;
            string error1 = "";
            string error2 = "";
            if (paraCRJ.Enabled)
            {
                QCHandler h = Handles[paraCRJ.HandleType()];
                bRet0 = h.Init();
                error1 = h.ErrorInfo;
            }
            if (paraVisa.Enabled)
            {
                QCHandler h = Handles[paraVisa.HandleType()];
                bRet1 = h.Init();
                error2 = h.ErrorInfo;
            }
            _error = error1 + error2;
            _bInited = (bRet0 && bRet1);
            return _bInited;
        }

        public void Dispose()
        {
            foreach (KeyValuePair<int, QCHandler> p in Handles)
            {
                QCHandler handler = p.Value;
                if (handler.HasInited)
                {
                    handler.Uninit();
                }
            }
            Handles.Clear();
        }
    }
}
